/**
 *	Contains the following items specific to 'noughts and crosses':
 *		- Rules
 *		- Win conditions
**/

"use strict"


/**
 *	Add game rules
**/
GameLogic.validator.addRules({
	
	checkGameAlreadyWon: function(game, move) {
		if (game.winner !== 0) {
			var msg = ( 
				game.winner === -1 ? 
				"the game has ended in a draw" : 
				"the game was already won by " + game.players[ game.winner-1 ] 
				)
			return {
				success: false,
				message: msg
			}
		}
		return { success: true }
	},
	
	checkSquareAlreadyFilled: function(game, move) {
		var y = move.y, x = move.x
		
		if (game.state[y][x] !== 0) {
			return {
				success: false,
				message: "square already occupied"
			}
		}
		return { result: true }
	},
		
})


/**
 *	Add win conditions
**/
GameLogic.winChecker.addRules({
	
	// returns -1 (draw) if all squares are full
	checkAllSquaresFull: function(game) {
		var state = game.state
		
		for (var y=0, yy=state.length; y<yy; y++) {
			for (var x=0, xx=state[0].length; x<xx; x++) {
				 if (state[y][x] == 0)  return 0			//0 signifies no winner found
			}
		}
		return -1											//-1 signifies a draw
	},
	
	
	// returns the winner if a 'full' row was filled.
	checkFullRow: function(game) {
		// uses a meta rule from 'rules/NCL.js'
		return this.metaCheckRowOfX(game, game.board.w)
	},
	
	
	// returns the winner if a 'full' column was filled
	checkFullColumn: function(game) {
		// uses a meta rule from 'rules/NCL.js'
		return this.metaCheckColumnOfX(game, game.board.h)
	},

	// checks both diagonals, and returns the winner if either are full	
	checkFullDiagonals: function(game) {
		// define the cross size as the smallest of the 2 dimensions, in case of rectangular board
		var crosssize = ( game.board.h < game.board.w ? game.board.h : game.board.w )
		
		// uses a meta rule from 'rules/NCL.js'
		return this.metaCheckDiagonalOfX(game, 0, 0, crosssize)
	}
	
})